import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:interview/components/app/app_confirm_dialog.dart';
import 'package:interview/components/app/app_ripple_button.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/controller/interview_tabs_controller.dart';
import 'package:interview/theme/app_colors.dart';

class InterviewKeywords extends StatefulWidget {
  final AppColors theme;
  final Function(String tab) onTap;
  const InterviewKeywords({super.key, required this.theme, required this.onTap});

  @override
  State<InterviewKeywords> createState() => _InterviewKeywordsState();
}

class _InterviewKeywordsState extends State<InterviewKeywords> {
  final interviewTabsController = Get.find<InterviewTabsController>();






  Future <void> _onLongPress (String tab) async {
    bool? confirm = await AppConfirmDialog.show(
      context: context, 
      theme: widget.theme, 
      title: "添加",
      content: Text("是否把 $tab 添加至分类"), 
      confirmText: "添加",
      cancelText: "关闭",
    );
    if(confirm != null){
      if(confirm){
        interviewTabsController.addTab(tab);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.theme.subjectMix1,
      padding: EdgeInsets.symmetric(horizontal: 6 , vertical: 6),
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal, // 横向滚动
        child: Row(
          children: List.generate(interviewTabsController.unselectedKeywords.toList().length, (index){
            final item = interviewTabsController.unselectedKeywords.toList()[index];
            return Padding(
              padding: EdgeInsets.only(right: 8), // 添加每个item右侧的间隙
              child: _buildItem(
                theme: widget.theme,
                text: item,
                path: interviewTabsController.getTabIconPath(item),
              ),
            );
          })
        ),
      ),
    );
  }


  Widget _buildItem({
    required AppColors theme,
    required String text,
    required String path,
  }){
    return AppRippleButton(
      onTap:() => widget.onTap(text),
      onLongPress: () => _onLongPress(text),
      rippleRadius: BorderRadius.circular(4),
      borderRadius:BorderRadius.circular(4) ,
      backgroundColor: theme.subjectPure!,
      highlightColor: theme.contrastMix3!.withOpacity(0.2),
      splashColor:  theme.contrastMix3!.withOpacity(0.2),
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 6 , vertical: 12),
        child: Column(
          children: [
            SvgPicture.asset("${AppEnumAssets.basePath}$path" ,width: 28 ,height: 28,),
            SizedBox(height: 6,),
            Text("$text" , style: TextStyle(fontSize: 12 , fontWeight: FontWeight.w500, color: theme.contrastMix1),),
            Container(
              decoration: BoxDecoration(
                color: theme.subjectMix1,
                borderRadius: BorderRadius.circular(6)
              ),
              margin: EdgeInsets.symmetric(vertical: 6),
              padding: EdgeInsets.symmetric(vertical: 4 , horizontal: 24), 
              child: Text("查看", style: TextStyle(fontSize: 10 , fontWeight: FontWeight.w500 , color: theme.contrastMix3),),
            )
          ],
        ),
      )
    );
  }
}